﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineJobPortal.Models;
using System.Data.Entity;
using System.Data;
using System.Web.DataAccess;
using System.Web.Security;
using System.Web.Routing;
namespace JobPortal.Controllers
{
    public class JobController : Controller
    {
        JobPortalEntities7 objEntities = new JobPortalEntities7();
        IJobRepository job = null;
        public JobController()
        {
        }
        public JobController(IJobRepository job)
        {
            this.job = job;
        }
        public ActionResult Home()
        {
            object value = Session["email"];
            if (value != null)
            {
                return View();
            }
            return View("~/Views/Home/Index.cshtml");
            
        }
        public ActionResult AddJob()
        {
            object value = Session["email"];
            if (value != null)
            {
                ViewBag.userid = Session["userid"]; // this is company id
                return View();
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult SaveJob(Job jobb)
        {
            object session_id = Session["userid"];
            int id = (int)session_id;
            jobb.CompanyID = id;
            jobb.CompanyName = Session["name"].ToString();
            job.SaveJob(jobb);
            return RedirectToAction("Home");
        }
        public ActionResult EditJob(int id=0)
        {
            Job j = job.Find_Job_id(id);
            if (j == null)
            {
                return HttpNotFound();
            }
            ViewBag.category = j.JobCategory;
            ViewBag.type = j.JobType;
            ViewBag.shift = j.Shift_Timings;
            ViewBag.qualifi = j.Req_Qualification;
            ViewBag.experi = j.Req_Experience;
            // ye object model mai milay ga
            return View(j); 
        }
        public ActionResult EditJob1() 
        {
          object value = Session["email"];
          if (value != null)
          {
              object session_id = Session["userid"];
              int CompanyID = (int)session_id;
              ViewBag.jobsData = job.SearchByID(CompanyID);
              return View();
          }
          return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult UpdateJob(Job j)
        {
            job.UpdateJob(j);
            return RedirectToAction("Home");
        }
       
        public ActionResult DeleteJob()
        {
          object value = Session["email"];
          if (value != null)
          {
              object session_id = Session["userid"]; // this is company id
              int id = (int)session_id;
              ViewBag.jobsData = job.SearchByID(id);
              return View();
          }
          return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult DeleteJob1(int id)
        {
            job.DeleteJob(id);
            return RedirectToAction("Home");
        }
        public ActionResult SeekEmployees()
        {
            ViewModel model = new ViewModel();
            object session_id = Session["userid"];
            int userid = (int)session_id;
            string c_name = Session["name"].ToString();
            object value = Session["email"];
            if (value != null)
            {
                var emplist = from apply in objEntities.ApplyforJobs
                              join user in objEntities.Users
                              on apply.UserID equals user.UserId
                              join job in objEntities.Jobs
                              on apply.JobID equals job.JobID
                              where job.CompanyName.Equals(c_name) && apply.Status.Equals("Pending")
                              select new ViewModel
                              {
                                  User = user,
                                  Job = job,
                                  ApplyforJob = apply
                              };
                ViewBag.x = c_name;
                return View(emplist);
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult selected()
        {
            var applyList = objEntities.ApplyforJobs.ToList();
            List<String> selected = Request["selectedEmp"].Split(',').ToList();
            foreach (var i in selected)
            {
                foreach (var j in applyList)
                {
                    if (i.Equals(j.ApplyID.ToString()))
                    {
                        ApplyforJob a = objEntities.ApplyforJobs.Find(j.ApplyID);
                        a.Status = "Selected";
                        objEntities.Entry(a).State = EntityState.Modified;
                        objEntities.SaveChanges();
                    }
                }
            }
            return View();
        }


       
    }
}
